//
//  RadioPlayTool.m
//  Car
//
//  Created by 何晓文 on 15/9/17.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "RadioPlayTool.h"
#define kRadioM3u8 @"http://live.fm.autohome.com.cn/auto.m3u8"
@implementation RadioPlayTool

static RadioPlayTool *playTool;
+ (RadioPlayTool *)shareRadioTool {
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		if (!playTool) {
			playTool = [[RadioPlayTool alloc] init];
		}
	});
	return playTool;
}
//获取当前时间
- (NSString *)getCurrentTime {
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSString *time = [formatter stringFromDate:[NSDate date]];
	NSLog(@"%@",time);
	
	return time;
}
//比较列表时间和当前时间
- (void)compareTime {
	NSString *currentTime = [self getCurrentTime];
	for (RadioListModel *model in self.radioList) {
		if ([currentTime compare: model.starttime] >= 0 && [currentTime compare: model.endtime] <= 0) {
			self.radioModel = model;
			
		}
	}
	
}
//解析电台节目
- (void)initData {
	
	[LORequestManger GET:kRadioURL success:^(id response) {
		NSDictionary *dict = (NSDictionary *)response;
		NSDictionary *result = dict[@"result"];
		for (NSDictionary *list in result[@"list"]) {
			RadioListModel *model = [RadioListModel shareRadioModelWithDictionary:list];
			[self.radioList addObject:model];
			
		}
		[self playWithRadioURl:kRadioM3u8];

	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		NSLog(@"%@", error);
 }];
	
}
- (NSMutableArray *)radioList {
	if (!_radioList) {
		_radioList = [[NSMutableArray alloc] init];
	}
	return  _radioList;
}
//懒加载播放器
- (AVPlayer *)player {
	if (!_player) {
		_player = [[AVPlayer alloc] init];
	}
	return _player;
}
//播放方法
- (void)playWithRadioURl:(NSString *)url {
	if (self.isPlaying) {
		return;
	}
	//清空观察者
	if (self.playerItem) {
		[self.playerItem removeObserver:self forKeyPath:@"status"];
	

	}
	//初始化item
	self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
	
	[self.playerItem addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
	
	NSLog(@"%@", self.playerItem);
	[self.player replaceCurrentItemWithPlayerItem:self.playerItem];
}

//观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	if ([keyPath isEqualToString:@"status"]) {
		switch ([change[@"new"] integerValue]) {
			case AVPlayerItemStatusReadyToPlay:
				NSLog(@"缓冲成功");
				self.readyToPlay = YES;
				[[NSNotificationCenter defaultCenter] postNotificationName:@"readyToPlay" object:nil];
				[ProgressHUD showSuccess:@"加载完成"];
				break;
			case AVPlayerItemStatusUnknown:
				NSLog(@"未知错误");
				break;
			case AVPlayerStatusFailed:
				NSLog(@"缓冲失败");
				[ProgressHUD showError:@"网络错误"];
				break;

	}
}


}
//播放按钮
- (void)startPlayRadio {
	
	if (self.readyToPlay == YES) {
		[self.player play];
		self.isPlaying = YES;
	}
	NSLog(@"节目开始");
}
//暂停按钮
- (void)radioPause {
	if (self.isPlaying == YES) {
		[self.player pause];
		self.isPlaying = NO;
	}
	NSLog(@"节目暂停");

}
@end
